<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>天天生鲜-支付管理</title>
<link rel="shortcut icon" href="images/copylogo.png">
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/main.css">
<link rel="stylesheet" href="css/pay.css">
<link rel="stylesheet" href="css/foot.css">
</head>

<body>
<div id="header">
	<!-- 头部  -->
	<div class="header_con">
    	<div class="header">
    		<div class="welcome fl">欢迎来到天天生鲜</div>
    		<div class="fr">
                <div class="login_btn fl" v-if="checkLogin">
                	欢迎：<b>{{user.nickName}}</b><span> |</span><a href="register.html">注册</a>
                </div>
                <div class="login_btn fl" v-else>
                	<a href="login.html">登录</a><span> | </span><a href="register.html">注册</a>
                </div>
            	 <div class="user_link fl">
                    <span> | </span>
                    <a href="user.html">用户中心</a>
                    <span> | </span>
                    <a href="cart.html" target="_blank">我的购物车</a>
                    <span> | </span>
                    <a href="order.html" target="_blank">我的订单</a>
           		 </div> 
             </div>  
    	</div>
    </div>
    
     <!-- 搜素框 -->
     <div class="search_bar clearfix">
 		<a href="index.html" class="logo fl"><img src="images/idid_ifrtgzddgm3dmnjshezdambqhayde_49x48.png"><h5>美丽说</h5></a>
 		<div class="search_con fl">
        	<form>
                <input type="text" class="input_text fl" name="key_words" placeholder="搜索商品"/>
                <input type="submit" class="input_btn fr"  value="搜索"/>
            </form>
        </div>
 		<div class="guest_cart fr">
        	<a href="cart.html" class="cart_name fl" target="_blank">我的购物车</a>
            <div class="goods_count fl" id="show_count" v-html="Object.keys(carts).length"></div>
        </div>
     </div>
</div>
<div id="app">
     <!-- 收货地址 -->
    <h3 class="common_title">收货地址</h3>

	<div class="common_list_con clearfix">
		<input type="hidden" id="receive_addr" />
		<dl id="addr_list">
			<dt>寄送到：</dt>
			<!-- :class="{addr.status == 1 ? '': ''}" 
				:class="{selectedAddr:addr.flag == 1}"
			-->
			<dd v-for="item in addrs" :data-aid="item.aid">
				<span>邮寄到 </span>{{item.province}}&nbsp;{{item.city}}&nbsp;{{item.area}}
				&nbsp;{{item.addr}}&nbsp;（{{item.aname}}&nbsp;收）&nbsp; {{item.tel}}
			</dd>
		</dl>
		<a href="javascript:showAddrDiv()" class="edit_site">编辑收货地址</a>
	</div>
	
	<div id="addr_div">
		<img src="images/close.png" title="关闭" onclick="hiddenDiv()" />
		<form id="myform">
			<ul>
				<li>
					<label for="addr_name">收货人：</label>
					<input id="addr_name" name="name" type="text" placeholder="请输入收货人姓名"/>
				</li>
				<li>
					<label for="addr_tel">联系方式：</label>
					<input id="addr_tel" name="tel" type="tel" placeholder="请输入收货人联系方式" />
				</li>
				<li>
					<label for="addr_name">收货地址：</label>
					<select id="province" name="province"></select>
					<select id="city" name="city">
						<option value="0">--请选择城市--</option>
					</select>
					<select id="area" name="area">
						<option value="0">--请选择地区--</option>
					</select>
				</li>
				<li>
					<label for="addr_addr">详细地址：</label>
					<input id="addr_addr" name="addr" style="width: 480px;" type="text" placeholder="请输入详细地址（街道、门牌等）" />
				</li>
				<li class="addr_btn">
					<a href="javascript:addAddr()">添加收货地址</a>
				</li>
			</ul>
		</form>
	</div>
    
   	<!-- 支付方式 -->	
    <h3 class="common_title">支付方式</h3>	
	<div class="common_list_con clearfix">
		<div class="pay_style_con clearfix" @click="up">
			<input type="radio" name="pay_style">
			<label class="cash">货到付款</label>
			<input type="radio" name="pay_style">
			<label class="weixin">余额支付</label>
			<input type="radio" name="pay_style" checked>
			<label class="zhifubao"></label>
		</div>
	</div>
    
   	<!-- 商品列表 -->
    <h3 class="common_title">商品列表</h3>
	<div class="common_list_con clearfix" id="order_list">
		<ul class="goods_list_th clearfix">
			<li class="col01">商品名称</li>
			<li class="col02">商品净重/单位</li>
			<li class="col03">商品价格</li>
			<li class="col04">数量</li>
			<li class="col05">小计</li>		
		</ul>
		<ul class="goods_list_td clearfix" v-for="(item, index) in orders">
			<li class="col01">{{index + 1}}</li>			
			<li class="col02"><img :src="item.pics"></li>
			<li class="col03">{{item.pname}}</li>
			<li class="col04">{{item.unit}}</li>
			<li class="col05">{{item.price}}元</li>
			<li class="col06">{{item.num}}</li>
			<li class="col07" v-html="(item.price * item.num).toFixed(2) + '元'"></li>	
		</ul>
	</div>
     
    <!-- 金额结算 -->	
    <h3 class="common_title">总金额结算</h3>
	<div class="common_list_con clearfix">
		<div class="settle_con">
			<div class="total_goods_count">共<em id="total_count">{{totalCount}}</em>件商品，总金额<b id="total_price">{{totalPrice}}元</b></div>
			<div class="transit">运费：<b>10元</b></div>
			<div class="total_pay">实付款：<b id="real_price">{{totalPrice+10}}元</b></div>
		</div>
	</div>
	<div class="order_submit clearfix">
		<a href="javascript:void(0);" id="order_btn" onclick="payOrder()">提交订单</a>
	</div>	
    
    <div class="popup_con">
		<div class="popup">
			<p>订单提交成功！</p>
		</div>
		
		<div class="mask"></div>
	</div>
</div>       
<!-- 版权所有 -->
<div class="footer">
	<div class="foot_link">
		<a href="#">关于我们</a> <span> | </span>
        <a href="#">联系我么</a> <span> | </span>
        <a href="#">招聘人才</a> <span> | </span>
        <a href="#">友情链接</a> 
	</div>
	<p>CcpyRight &copy; 2019 衡阳市源辰信息科技有限公司 All Rights Reserverd</p>
    <p>电话：0734-8355998 湘ICP备16015987号</p>
</div>
    
<script type="text/javascript" src="js/jquery-3.4.1.min.js"></script>
<script type="text/javascript" src="js/addr.js"></script>
<script type="text/javascript" src="js/vue.js"></script>
<script type="text/javascript" src="js/axios.js"></script>
<script type="text/javascript" src="js/qs.js"></script>
<script type="text/javascript" src="js/show-dialog.js"></script>
<script type="text/javascript" src="js/header.js"></script>
<script type="text/javascript">

$(function(){
	$("#addr_addr").keyup(function(event) {
		if (event.which == 13) {
			addAddr();
		}
	})
	
	$("#addr_list>dd").click(function(){
		$("#addr_list>dd").removeClass("selectedAddr");
		$(this).addClass("selectedAddr");
		
	});
})

function showAddrDiv() {
	location.href="address.html";
}

function hiddenDiv(){
	$("#addr_div").css("display", "none");
}

function addAddr() {
	let objs = $("#myform input, #myform select");
	for (let i = 0, len = objs.length; i < len; ++i) {
		if ($.trim(objs[i].value) == "" || objs[i].value == "--请选择省份--") {
			showmsg("您输入的信息不完整，请完善后重新提交...", "red");
			return;
		}
	}
	
	$.post("addr/add", $("#myform").serialize(), rt => {
		if (rt.code == 200) {
			let obj = {ano: rt.data};
			obj.name = $.trim($("#addr_name").val());
			obj.tel = $.trim($("#addr_tel").val());
			obj.province = $.trim($("#province").val());
			obj.city = $.trim($("#city").val());
			obj.area = $.trim($("#area").val());
			obj.addr = $.trim($("#addr_addr").val());
			obj.flag = 0;
			app.$data.addrs.push(obj);
			$("#myform")[0].reset();
			hiddenDiv();
			return;
		}
		showmsg("收获地址添加失败，请稍后重试...", "red");
	}, "json");
}

function bindInfo() {
	$("#addr_list>dd").click(function(){
		$("#addr_list>dd").removeClass("selectedAddr");
		$(this).addClass("selectedAddr");
	});
}
let app = new Vue({
	el:"#app",
	data:{
		orders:[],
		addrs:[],
		totalCount: 0,
		totalPrice: 0,
		totalPrices:0,
		usermoney:-1
	},
	watch:{
		addrs:{
			handler(newData, oldData){
				this.$nextTick(function(){
					bindInfo();
				})
			},
			deep: true
		}
	},
	methods:{
		up: function(){
			let select = $("[name = 'pay_style']").filter(":checked");
			console.log(select,"select");
		}
	},
	mounted: function(){
		let pnos = localStorage.getItem("pnos");
		if(!pnos){
			showmsg("请选中您要支付的商品信息....","red",function(){location.href="/order/cart.html"});
			return;
		}
		
		//查商品信息
		axios.post("/cart/findByPnos",qs.stringify({pnos: pnos})).then(rt =>{
			if(rt.status == 200){
				if(rt.data.code == 200){
					this.orders = rt.data.data;
					console.log(this.orders,"this.order");
					this.orders.forEach((item, index) =>{
						this.totalCount += item.num;
						this.totalPrice += item.num * Number(item.price);
					})
					this.totalPrice = parseFloat(this.totalPrice.toFixed(2));
					return;
				}
				this.orders = [];
				
				if(rt.data.code == 511){
					showmsg("请先登录...","yellow",function(){location.href="login.html"});
					return;
				}
				
				if(rt.data.code == 512){ //未找到  NOTFOUND
					alert(rt.data.message);
					return;
				}
				
				if(rt.data.code == 600){ //没有资源  NOT_DATA
					alert(rt.data.message);
					return;
				}
			}
		
		})
		
		axios.get("/addr/list").then(rt =>{
			if(rt.status == 200 && rt.data.code == 200){
				this.addrs = rt.data.data;
				this.$nextTick(function (){
					bindInfo();
				})
				return;
			}
			this.carts = [];
			if(rt.data.code == 512){ //未找到  NOTFOUND
				alert(rt.data.message);
				return;
			}
			
		})
	},
	created(){
		axios.post("/user/member/checklogin",null).then(rt=>{
			if(rt.data.code == 200){
				this.mid = rt.data.data.mid;
				this.usermoney = rt.data.data.money;
				return;
			}
			if(rt.data.code == 511){
				confirm(rt.data.message);
				location.href="/user/login.html";
			}
		})
	}
})

function payOrder(){
	if(app.$data.usermoney < 0){
		showmsg("出错...","red",function(){location.reload()});
		return;
	}
	if(app.$data.usermoney < app.$data.totalPrice){
		showmsg("余额不足...","red",function(){});
		return;
	}
	let pnos = [];
	app.$data.orders.forEach((item, index) =>{
		pnos.push(item.bid);
	});

	let ano = $("#addr_list>dd[class='selectedAddr']").data("aid");
	if(!ano){
		showmsg("请选择您的收货地址....","red");
		return;
	}
	let total = app.$data.totalPrice;
	let infos = {"pnos": pnos,"aid" : ano,totalprice: total};
	$.post("/order/order/addlist",qs.stringify(infos), rt=>{
		if(rt.code == 200 ){
			localStorage.removeItem("pnos");
			console.log(rt.data.oid,rt.data.totalprice);
			showmsg("下单成功,请支付...","green", function(){
				location.href="/alipay/pay" +"/" + rt.data.oid + "/" + rt.data.totalprice;// +"/" + rt.data.mid;
			});
			return;
		}
		
		if(rt.code == 511){ //未登录  NOTLOGIN
			alert(rt.message);
			return;
		}
		
		if(rt.code == 513){ // 信息不完整  INCOMPLETEINFORMATION
			alert(rt.message);
			return;
		}
		
		if(rt.code == 601){
			showmsg("库存不足...","red");
			return;
		}
		showmsg("下单失败，请稍后重试....","red");
	},"json");
}
</script>
</body>
</html>